<?php
/**
 * Desc:
 * User: 叶力铭
 */

class DigitalSignApi
{
    private $domain = 'http://183.230.194.50:10098';
    private $localDomain = 'http://127.0.0.1:8080';

    private $appId = '1603165891824';
    private $appKey = 'nx3eWRuSjORxgZqyFoF09KNy';
    private $appSecret = 'tJ04tmCOBTo3UKTQu5pB4qji14z0puLD';

    public function __construct()
    {

    }

    /**
     * Desc: 授权获取token
     * User: 叶力铭
     * @return bool
     */
    public function getToken()
    {

        $url = $this->domain.'/open/oauth/token';

        $body['client_id'] = $this->appKey;
        $body['client_secret'] = $this->appSecret;
        $body['grant_type'] = 'client_credentials';

//        $url = $this->setUrlQuery($url, $query);
//        $header['Content-Type'] = 'application/x-www-form-urlencoded';
        return $this->sendCurl($url, http_build_query($body), []);

    }

    /**
     * Desc: 创建电子印章
     * User: 叶力铭
     * @param $param
     * @return bool
     */
    public function createSeal($param)
    {
        $url = $this->localDomain.'/sdk/signature/signature/v1/seal/create';
        list($bool, $tokenRet) = $this->getToken();

        $header = [
            'Content-Type: multipart/form-data',
            'Authorization:Bearer ' . $tokenRet['access_token']
        ];

        $body['username'] = $param['username'];
        $body['sealtype'] = $param['seal_type'];
        $body['usertype'] = $param['user_type'];
        $body['userid'] = $param['user_sn'];
        $body['projectid'] = $this->appId;
        $body['businessid'] = $param['business_id'];
        if (2 == $body['userid']){
            $body['sealnumber'] = $body['userid'];
        }

        return $this->sendCurl($url, $body, $header);
    }

    /**
     * Desc: 通过图片创建电子印章
     * User: 叶力铭
     * @param $param
     * @return bool
     */
    public function createSealByPicture($param)
    {
        $url = $this->localDomain.'/sdk/signature/signature/v1/seal/create/picture';

        list($bool, $tokenRet) = $this->getToken();

        $header = [
            'Content-Type: multipart/form-data',
            'Authorization:Bearer ' . $tokenRet['access_token']
        ];

        $body['username'] = $param['username'];
        $body['sealtype'] = $param['seal_type'];
        $body['usertype'] = $param['user_type'];
        $body['userid'] = $param['user_sn'];
        $body['projectid'] = $this->appId;
        $body['businessid'] = $param['business_id'];
        if (2 == $body['userid']){
            $body['sealnumber'] = $body['userid'];
        }

        $body['sealpicture'] = $param['sal_picture'];

        return $this->sendCurl($url, $body, $header);
    }

    /**
     * Desc: 文件盖章签名
     * User: 叶力铭
     * @param $param
     * @return bool
     */
    public function toSeal($param)
    {
        $url = $this->localDomain.'/sdk/signature/signature/v1/seal/cert/sign';
        list($bool, $tokenRet) = $this->getToken();

        $header = [
            'Content-Type: multipart/form-data',
            'Authorization:Bearer ' . $tokenRet['access_token']
        ];

        $body['sealid'] = $param['seal_id'];
        $body['businessid'] = $param['businessid'];
        $body['projectid'] = $this->appId;
        $body['pdfPath'] = $param['pdf_path'];
        $body['signposition'] = '1,0,0';
        $body['beprotected'] = $param['beprotected'];

        return $this->sendCurl($url, $body, $header);
    }

    /**
     * Desc: 人脸验证
     * User: 叶力铭
     * @param $param
     * @return bool
     */
    public function face($param)
    {
        $url = $this->domain.'/cloud/face/v1/h5/face';
        list($bool, $tokenRet) = $this->getToken();

        $header = [
            'Content-Type: multipart/form-data',
            'Authorization:Bearer ' . $tokenRet['access_token']
        ];


        $body['url'] = $param['redirect_url'];
        $body['resultType'] = $param['result_type'];
        $body['orderNo'] = $param['order_no'];
        $body['name'] = $param['name'];
        $body['idCard'] = $param['id_card'];

        return $this->sendCurl($url, $body, $header);

    }

    /**
     * Desc: 组装query参数
     * User: 叶力铭
     * @param $url
     * @param $queryAry
     * @return string
     */
    private function setUrlQuery($url, $queryAry){
        if (strpos($url,'?')){
            $url = $url . '&' . http_build_query($queryAry);
        }else{
            $url = $url . '?' . http_build_query($queryAry);
        }
        return $url;
    }

    /**
     * Desc: 发送请求
     * User: 叶力铭
     * @param $url
     * @param $sendData
     * @param $headers
     * @param int $type 1post 其他get
     * @param int $second
     * @return bool
     */
    private function sendCurl($url, $sendData, $headers, $type=1, $second = 30)
    {
        $ch = curl_init();

        if (!empty($headers)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT,$second);

        if (1 == $type){
            //post提交方式
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
        }

        //运行curl
        $data = curl_exec($ch);

        $dataArr = json_decode($data, true);
        if ($dataArr) {
            curl_close($ch);
            return [true, $dataArr];
        }else{
            $error = curl_errno($ch);
            curl_close($ch);
            return [false, $error];
        }
    }
}